<template>
    <a-drawer
        :title="formData.id ? '编辑我的考试历史' : '增加我的考试历史'"
        :width="600"
        :visible="visible"
        :destroy-on-close="true"
        :footer-style="{ textAlign: 'right' }"
        @close="onClose"
    >
        <a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
            <a-form-item label="考试ID：" name="examId">
                <a-input v-model:value="formData.examId" placeholder="请输入考试ID" allow-clear />
            </a-form-item>
            <a-form-item label="用户ID：" name="userId">
                <a-input v-model:value="formData.userId" placeholder="请输入用户ID" allow-clear />
            </a-form-item>
            <a-form-item label="阅卷用户ID：" name="markUserId">
                <a-input v-model:value="formData.markUserId" placeholder="请输入阅卷用户ID" allow-clear />
            </a-form-item>
            <a-form-item label="考试开始时间：" name="answerStartTime">
                <a-date-picker v-model:value="formData.answerStartTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择考试开始时间" style="width: 100%" />
            </a-form-item>
            <a-form-item label="考试结束时间：" name="answerEndTime">
                <a-date-picker v-model:value="formData.answerEndTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择考试结束时间" style="width: 100%" />
            </a-form-item>
            <a-form-item label="阅卷开始时间：" name="markStartTime">
                <a-date-picker v-model:value="formData.markStartTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择阅卷开始时间" style="width: 100%" />
            </a-form-item>
            <a-form-item label="阅卷结束时间：" name="markEndTime">
                <a-date-picker v-model:value="formData.markEndTime" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择阅卷结束时间" style="width: 100%" />
            </a-form-item>
            <a-form-item label="客观题分数：" name="objectiveScore">
                <a-input v-model:value="formData.objectiveScore" placeholder="请输入客观题分数" allow-clear />
            </a-form-item>
            <a-form-item label="总分数：" name="totalScore">
                <a-input v-model:value="formData.totalScore" placeholder="请输入总分数" allow-clear />
            </a-form-item>
            <a-form-item label="状态（1：未考试；2：考试中；3：已交卷；）：" name="status">
                <a-input v-model:value="formData.status" placeholder="请输入状态（1：未考试；2：考试中；3：已交卷；）" allow-clear />
            </a-form-item>
            <a-form-item label="阅卷状态（1：未阅卷；2：阅卷中；3：已阅卷；）：" name="markState">
                <a-input v-model:value="formData.markState" placeholder="请输入阅卷状态（1：未阅卷；2：阅卷中；3：已阅卷；）" allow-clear />
            </a-form-item>
            <a-form-item label="答题状态（1：及格；2：不及格）：" name="answerState">
                <a-input v-model:value="formData.answerState" placeholder="请输入答题状态（1：及格；2：不及格）" allow-clear />
            </a-form-item>
            <a-form-item label="排序：" name="sortCode">
                <a-input v-model:value="formData.sortCode" placeholder="请输入排序" allow-clear />
            </a-form-item>
            <a-form-item label="版本：" name="ver">
                <a-input v-model:value="formData.ver" placeholder="请输入版本" allow-clear />
            </a-form-item>
        </a-form>
        <template #footer>
            <a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
            <a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
        </template>
    </a-drawer>
</template>

<script setup name="exmMyExamHisForm">
    import { cloneDeep } from 'lodash-es'
    import { required } from '@/utils/formRules'
    import exmMyExamHisApi from '@/api/biz/exmMyExamHisApi'
    // 抽屉状态
    const visible = ref(false)
    const emit = defineEmits({ successful: null })
    const formRef = ref()
    // 表单数据
    const formData = ref({})
    const submitLoading = ref(false)

    // 打开抽屉
    const onOpen = (record) => {
        visible.value = true
        if (record) {
            let recordData = cloneDeep(record)
            formData.value = Object.assign({}, recordData)
        }
    }
    // 关闭抽屉
    const onClose = () => {
        formRef.value.resetFields()
        formData.value = {}
        visible.value = false
    }
    // 默认要校验的
    const formRules = {
    }
    // 验证并提交数据
    const onSubmit = () => {
        formRef.value
            .validate()
            .then(() => {
                submitLoading.value = true
                const formDataParam = cloneDeep(formData.value)
                exmMyExamHisApi
                    .exmMyExamHisSubmitForm(formDataParam, formDataParam.id)
                    .then(() => {
                        onClose()
                        emit('successful')
                    })
                    .finally(() => {
                        submitLoading.value = false
                    })
            })
    }
    // 抛出函数
    defineExpose({
        onOpen
    })
</script>
